﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework.Domain;
using System.Reflection;

namespace Framework.Event
{
    public sealed class InlineDomainEventHandler
    {
        private readonly Func<IDomainEvent, object> _Action;

        public InlineDomainEventHandler(IEntity entity, MethodInfo mi)
        {
            ParameterInfo[] parameters = mi.GetParameters();
            if (parameters == null || parameters.Count() == 0)
            {
                throw new ArgumentException(string.Format("EX_INVALID_HANDLER_SIGNATURE {0}", mi.Name), "mi");
            }
            _Action = domainEvent =>
            {
                return mi.Invoke(entity, new object[] { domainEvent });
            };
        }

        #region IHandler<IDomainEvent> Members

        public void Handle(IDomainEvent message)
        {
             _Action(message);
        }

        #endregion
    }
}
